package namespace.jcu.mobile.library.system;

import java.util.ArrayList;

import com.parse.Parse;
import com.parse.ParseUser;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class MyAccountActivity extends Activity {

	Button login;
	Button logout;

	@Override
    public void onCreate(Bundle savedInstanceState) {
		Parse.initialize(this, "1GlGZQ1XWU34mHgFyDHZiZRr6HTTSWOZo2nSAowy", "GKVclKTd6TIPxLVPPbDiyvzAMoWA2XLf4ZdZYFwI");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_account);
        

        
        ArrayList<MyAccount> myAccountList = new ArrayList<MyAccount>();
        MyAccount currentTransaction = new MyAccount("Current Transaction", "Review, Renewal, Check Return Date");
        myAccountList.add(currentTransaction);
        MyAccount viewHistory = new MyAccount("View History", "Review of Past Loans");
        myAccountList.add(viewHistory);
        MyAccount addNews = new MyAccount("Add News", "Update News Announcement");
        myAccountList.add(addNews);
        
        ListView listView = (ListView) findViewById(R.id.ListViewId);
        listView.setAdapter(new UserItemAdapter(this, android.R.layout.simple_list_item_1, myAccountList));
        listView.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> arg0, View view, int arg2,long itemID) {
            	if (arg2 == 0)		//Current Transaction has been selected
            	{
            		Intent i = new Intent(MyAccountActivity.this, CurrentTransactionActivity.class);
            		startActivity(i);
            	}
            	else if (arg2 == 1)		//View History has been selected
            	{
            		Intent i = new Intent(MyAccountActivity.this, ViewHistoryActivity.class);
            		startActivity(i);
            	}
            	else if (arg2 == 2)		//View History has been selected
            	{
            		Intent i = new Intent(MyAccountActivity.this, AddNewsActivity.class);
            		startActivity(i);
            	}
            }
        });

        
        ParseUser currentUser = ParseUser.getCurrentUser();
        
        //log out button to display and its functions when user has already logged in
		if (currentUser != null)
		{
			logout = (Button) findViewById(R.id.loginButton);
			logout.setText("Logout");
			logout.setOnClickListener(new OnClickListener() {
				public void onClick(View v)
				{
					ParseUser user = ParseUser.getCurrentUser();
					user.logOut();
              		Context context = getApplicationContext();
              		CharSequence text = "You've Successfully Logged Out. Now Redirecting to Home Page ...";
              		int duration = Toast.LENGTH_LONG;

              		//toast message to display when the item is clicked
              		Toast toast = Toast.makeText(context, text, duration);
              		toast.show();
              		
              		//call home page class
					Intent i = new Intent(MyAccountActivity.this, HomeActivity.class);
					startActivity(i);
				}
			});
		}
		
		//log in button to display and its functions of navigating to login page when user hasn't logged in
		else
		{
	        login = (Button) findViewById(R.id.loginButton);
	        login.setOnClickListener(new OnClickListener() {
	        	public void onClick(View v)
	        	{
	        		//start executing another class LoginActivity
	        		Intent i = new Intent(MyAccountActivity.this, LoginActivity.class);
	        		startActivity(i);
	        	}
	        });
		}
		
    }
	
	public class UserItemAdapter extends ArrayAdapter<MyAccount> {
		private ArrayList<MyAccount> myAccountList;

		public UserItemAdapter(Context context, int textViewResourceId, ArrayList<MyAccount> myAccountList) {
			super(context, textViewResourceId, myAccountList);
			this.myAccountList = myAccountList;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View v = convertView;
			if (v == null) {
				LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				v = vi.inflate(R.layout.my_account_attribute_list, null);
			}
			
			MyAccount myaccount = myAccountList.get(position);
			if (myaccount != null) {
				TextView Attribute_Title = (TextView) v.findViewById(R.id.title);
				TextView Attribute_ShortDetail = (TextView) v.findViewById(R.id.detail);
				
				if (Attribute_Title != null) {
					Attribute_Title.setText(myaccount.Attribute_Title);
				}

				if(Attribute_ShortDetail != null) {
					Attribute_ShortDetail.setText(myaccount.Attribute_ShortDetail );
				}
				
				
			}
			return v;
		}
	}
	
	public class MyAccount {
		public String Attribute_Title;
		public String Attribute_ShortDetail;
		
		public MyAccount(String Attribute_Title, String Attribute_ShortDetail) {
			this.Attribute_Title = Attribute_Title;
			this.Attribute_ShortDetail = Attribute_ShortDetail;
		}
	}
}